package com.chushouya.controller.product.admin;

import com.chushouya.common.constant.ServiceType;
import com.general.framework.core.enums.BusinessType;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.Log;
import com.general.framework.web.annotations.RepeatSubmit;
import com.chushouya.product.dto.admin.productsku.ProductSkuListDTO;
import com.chushouya.product.dto.admin.productsku.ProductSkuPriceUpdateDTO;
import com.chushouya.product.dto.admin.productsku.SkuGenerateDTO;
import com.chushouya.product.service.admin.ProductSkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = ServiceType.PLATFORM + "商品SKU管理")
@RestController
@RequestMapping(ServiceType.PLATFORM + "/productSku")
public class ProductSkuController {

    @Resource
    private ProductSkuService productSkuService;


    @ApiOperation("删除商品SKU")
    @Log(businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuId", value = "商品SKU ID", required = true)
    })
    public Response<Void> remove(Long skuId) {
        productSkuService.removeProductSku(skuId);
        return Response.success();
    }

    @ApiOperation("根据商品ID获取SKU列表")
    @GetMapping("/listByProductId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "商品ID", required = true)
    })
    public Response<List<ProductSkuListDTO>> listByProductId(Long productId) {
        return Response.success(productSkuService.selectProductSkuListByProductId(productId));
    }

    @ApiOperation("生成SKU组合")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping("/generateSkuCombinations")
    @RepeatSubmit
    public Response<Integer> generateCombinations(@Validated @RequestBody SkuGenerateDTO skuGenerateDTO) {
        return Response.success(productSkuService.generateSkuCombinations(skuGenerateDTO));
    }

    @ApiOperation("更新SKU价格")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/updatePrice")
    @RepeatSubmit
    public Response<Boolean> updatePrice(@Validated @RequestBody ProductSkuPriceUpdateDTO priceUpdateDTO) {
        return Response.success(productSkuService.updateSkuPrice(priceUpdateDTO));
    }
}
